﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NH.OpenClosedPrinciple.SecondAttempt
{
	public class ProductFilter
	{
		public IEnumerable<Product> By(IList<Product> products, ProductFilterSpecification filterSpecification)
		{
			return filterSpecification.Filter(products);
		}
	}

	public abstract class ProductFilterSpecification
	{
		public IEnumerable<Product> Filter(IList<Product> products) {
			return ApplyFilter(products);
		}

		protected abstract IEnumerable<Product> ApplyFilter(IList<Product> products);
	}
	


	public class ColorFilterSpecification: ProductFilterSpecification
	{
		private readonly ProductColour productColour;
		public ColorFilterSpecification(ProductColour pProductColour)
		{
			productColour = pProductColour;
		}

		protected override IEnumerable<Product> ApplyFilter(IList<Product> products)
		{
			foreach (var product in products)
			{
				if (product.Colour == productColour)
					yield return product;
			}	
		}
	}

	public class SizeFilterSpecification: ProductFilterSpecification
	{
		private readonly ProductSize productSize;

		public SizeFilterSpecification(ProductSize productSize)
		{
			this.productSize = productSize;
		}

		protected override IEnumerable<Product> ApplyFilter(IList<Product> products) {
			foreach (var product in products)
			{
				if (product.Size == productSize)
					yield return product;
			}
		}
	}
}
